const md5 = require('md5');
const fs = require('fs');
const path = require('path');

function index(router) {
	router.post('/getdb', async (ctx) => {
		var param = ctx.request.body;
		try{
			var data = await ctx.db.get({
				dbname: param.dbname,
				field: param.field?param.field:'*',
				where: param.where?param.where:{}
			});
			ctx.response.body = {
					code:200,
					info: 'OK',
					lst: data
			};
		}catch(e){
			ctx.response.body = {
					code:500,
					info: '服务器错误',
			};
		}
		
	});
	router.get('/getdb', async (ctx) => {//直接获取数据表 dbname:数据表名 field:查询字段 默认*  where{}条件
		var param = ctx.request.query;
		try{
			var data = await ctx.db.get({
				dbname: param.dbname,
				field: param.field?param.field:'*',
				where: param.where?param.where:{}
			});
			ctx.response.body = {
					code:200,
					info: 'OK',
					lst: data
			};
		}catch(e){
			ctx.response.body = {
					code:500,
					info: '服务器错误',
			};
		}
		
	});
	router.post('/setdb', async (ctx) => {//直接运行mysql命令 参数query
		var param = ctx.request.body;
		try{
			var data = await ctx.db.query(param.query);
			ctx.response.body = {
					code:200,
					info: 'OK',
					lst: data
			};
		}catch(e){
			ctx.response.body = {
					code:500,
					info: '服务器错误',
			};
		}
		
	});
}
module.exports = index;
